/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: AccountInfoApi.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.sdk.account;

import android.text.TextUtils;

import io.bhex.baselib.network.params.IParams;
import io.bhex.baselib.network.response.ResponseListener;
import io.bhex.sdk.account.bean.AccountTypesResponse;
import io.bhex.sdk.account.bean.FindPasswordCheckRequest;
import io.bhex.sdk.account.bean.FindPasswordRequest;
import io.bhex.sdk.account.bean.FindPwdCheckRequest;
import io.bhex.sdk.account.bean.FindPwdCheckResponse;
import io.bhex.sdk.account.bean.FindPwdSend2FAVerifyCodeResponse;
import io.bhex.sdk.account.bean.MobileCodeListBean;
import io.bhex.sdk.account.bean.OrderParamResponse;
import io.bhex.sdk.account.bean.RequestIdResponse;
import io.bhex.sdk.account.bean.SetPasswdVerifyCodeResponse;
import io.bhex.sdk.account.bean.SubAccountBean;
import io.bhex.sdk.account.bean.SubAccountListResponse;
import io.bhex.sdk.account.bean.UserLevelInfoResponse;
import io.bhex.sdk.account.bean.UserVerifyInfo;
import io.bhex.sdk.Urls;
import io.bhex.baselib.constant.Fields;
import io.bhex.baselib.network.BParamsBuilder;
import io.bhex.baselib.network.HttpUtils;
import io.bhex.baselib.network.listener.SimpleResponseListener;
import io.bhex.baselib.network.params.GetParams;
import io.bhex.baselib.network.params.PostParams;
import io.bhex.baselib.network.response.ResultResponse;
import io.bhex.baselib.utils.MD5Utils;
import io.bhex.sdk.account.bean.VerifyFirstRequest;
import io.bhex.sdk.data_manager.TradeDataManager;
import io.bhex.sdk.security.bean.OrderIdParamResponse;
import io.bhex.sdk.trade.bean.AssetDataResponse;
import io.bhex.sdk.trade.bean.AssetListResponse;
import io.bhex.sdk.trade.bean.CanTransferAssetResponse;

public class AccountInfoApi {
    /**
     * 获取国家手机区号列表
     */
    public static void RequestGetMobileCodeList(String paramKeyType, SimpleResponseListener<MobileCodeListBean> listener) {
        GetParams params = BParamsBuilder.get()
                .url(Urls.API_MOBILE_CODE_LIST_URL)
                .addParam(paramKeyType.equals(Fields.FIELD_COUNTRY_PARAM_TYPE_AREA_CODE) ? Fields.COUNTRIES_AREA_CODE : Fields.COUNTRIES_NATIONALITY, true)
                .build();
        HttpUtils.getInstance().request(params, MobileCodeListBean.class, listener);
    }

    /**
     * 获取用户认证信息
     */
    public static void RequestGetUserVerifyInfo(SimpleResponseListener<UserVerifyInfo> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_USER_GET_VERIFY_INFO)
                .build();
        HttpUtils.getInstance().request(params, UserVerifyInfo.class, listener);
    }

    /**
     * RequestModifyPWD
     *
     * @param oldPwd
     * @param newPwd1
     * @param newPwd2
     * @param listener
     */
    public static void RequestModifyPWD(String oldPwd, String newPwd1, String newPwd2, SimpleResponseListener<ResultResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_USER_UPDATE_PWD)
                .addParam("old_password", MD5Utils.encode(oldPwd))
                .addParam("password1", MD5Utils.encode(newPwd1))
                .addParam("password2", MD5Utils.encode(newPwd2))
                .build();

        HttpUtils.getInstance().request(params, ResultResponse.class, listener);
    }

    /**
     * 设置密码-获取验证码
     *
     * @param listener
     */
    public static void RequestGetSetPWDVerifyCode(SimpleResponseListener<SetPasswdVerifyCodeResponse> listener) {

        PostParams params;
        params = BParamsBuilder.post()
                .url(Urls.API_SET_PASSWD_SEND_VERIFY_CODE_URL)
                .build();

        HttpUtils.getInstance().request(params, SetPasswdVerifyCodeResponse.class, listener);
    }

    /**
     * 设置密码
     *
     * @param password1
     * @param password2
     * @param verifyCode
     * @param orderId
     * @param listener
     */
    public static void RequestSetPWD(String password1,String password2, String verifyCode,String orderId,SimpleResponseListener<ResultResponse> listener) {

        PostParams params;
        params = BParamsBuilder.post()
                .url(Urls.API_SET_PASSWD_URL)
                .addParam("password1", MD5Utils.encode(password1))
                .addParam("password2", MD5Utils.encode(password2))
                .addParam("verify_code", verifyCode)
                .addParam("order_id", orderId)
                .build();

        HttpUtils.getInstance().request(params, ResultResponse.class, listener);
    }

    /**
     * RequestFindPWD
     *
     * @param request
     * @param listener
     */
    public static void RequestFindPWD(FindPasswordRequest request, SimpleResponseListener<ResultResponse> listener) {

        if (request == null)
            return;
        PostParams params;
        params = BParamsBuilder.post()
                .url(Urls.API_USER_FIND_PWD)
                .addParam("request_id", request.orderId)
                .addParam("password1", MD5Utils.encode(request.newPasswd))
                .addParam("password2", MD5Utils.encode(request.newPasswd2))
                .build();

        HttpUtils.getInstance().request(params, ResultResponse.class, listener);
    }

    /**
     * RequestFavoriteSymbol
     *
     * @param bCurrentFavorite //当前
     * @param exchageId
     * @param symbolId
     * @param listener
     */
    public static void RequestFavoriteSymbol(boolean bCurrentFavorite, String exchageId, String symbolId, SimpleResponseListener<ResultResponse> listener) {
        IParams params = BParamsBuilder.post()
                .url(bCurrentFavorite ? Urls.API_FAVORITE_CANCEL_URL : Urls.API_FAVORITE_CREATE_URL)
                .addParam(Fields.FIELD_EXCHANGE_ID2, exchageId)
                .addParam(Fields.FIELD_SYMBOL_ID, symbolId)
                .build();
        HttpUtils.getInstance().request(params, ResultResponse.class, listener);

    }

    /**
     * 快速登录-发送手机验证码 第一步
     *
     * @param mobile
     * @param token
     * @param listener
     */
    public static void requestMobileVerifyCodeOfQuickLogin(String mobile, String token,String deepknowId, String nationalCode, SimpleResponseListener<OrderIdParamResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_GET_BASIC_VERIFY_CODE_FIRST_MOBILE_URL)
                .addParam("type", "22") //快速登录
                .addParam("national_code", nationalCode)
                .addParam("mobile", mobile)
                .addParam("captcha_response", token)
                .addParam("captcha_id", deepknowId)
                .build();
        HttpUtils.getInstance().request(params, OrderIdParamResponse.class, listener);
    }

    /**
     * RequestVerifyFirst
     *
     * @param requestData
     * @param listener
     */
    public static void RequestVerifyFirst(VerifyFirstRequest requestData, ResponseListener<OrderParamResponse> listener) {
        if (requestData == null)
            return;

        IParams params;
        if (requestData.bEmail) {
            params = BParamsBuilder.post()
                    .url(Urls.API_GET_BASIC_VERIFY_CODE_FIRST_EMAIL_URL)
                    .addParam("type", "3")
                    .addParam("email", requestData.account)
                    .addParam("captcha_response", requestData.token)
                    .addParam("captcha_id", requestData.captcha_id)
                    .build();
        } else {
            params = BParamsBuilder.post()
                    .url(Urls.API_GET_BASIC_VERIFY_CODE_FIRST_MOBILE_URL)
                    .addParam("type", "3")
                    .addParam("national_code", requestData.mobileCode)
                    .addParam("mobile", requestData.account)
                    .addParam("captcha_response", requestData.token)
                    .addParam("captcha_id", requestData.captcha_id)
                    .build();
        }
        HttpUtils.getInstance().request(params, OrderParamResponse.class, listener);
    }

    /**
     * FindPwdCheck1
     *
     * @param requestData
     * @param listener
     */
    public static void FindPwdCheck1(FindPwdCheckRequest requestData, ResponseListener<FindPwdCheckResponse> listener) {
        if (requestData == null)
            return;

        IParams params;
        if (requestData.bEmail) {
            params = BParamsBuilder.post()
                    .url(Urls.API_EMAIL_FIND_PWD_CHECK_1)
                    .addParam("email", requestData.account)
                    .addParam("order_id", requestData.orderId)
                    .addParam("verify_code", requestData.verifyCode)
                    .build();
        } else {
            params = BParamsBuilder.post()
                    .url(Urls.API_MOBILE_FIND_PWD_CHECK_1)
                    .addParam("national_code", requestData.mobileCode)
                    .addParam("mobile", requestData.account)
                    .addParam("order_id", requestData.orderId)
                    .addParam("verify_code", requestData.verifyCode)
                    .build();
        }
        HttpUtils.getInstance().request(params, FindPwdCheckResponse.class, listener);
    }

    /**
     * FindPwdSend2FAVerifyCode
     * 发送找回密码的二次验证码(邮箱或者手机)
     *
     * @param requestId
     * @param listener
     */
    public static void FindPwdSend2FAVerifyCode(String requestId, ResponseListener<FindPwdSend2FAVerifyCodeResponse> listener) {
        if (TextUtils.isEmpty(requestId))
            return;

        IParams params = BParamsBuilder.post()
                .url(Urls.API_SEND_VERIFY_CODE_FIND_PWD)
                .addParam("request_id", requestId)
                .build();
        HttpUtils.getInstance().request(params, FindPwdSend2FAVerifyCodeResponse.class, listener);
    }

    /**
     * FindPwdCheck2FAVerifyCode
     * 找回密码step2-2FA验证
     *
     * @param requestId
     * @param listener
     */
    public static void FindPwdCheck2FAVerifyCode(String requestId, String orderId, String verifyCode, ResponseListener<ResultResponse> listener) {
        if (TextUtils.isEmpty(requestId))
            return;

        IParams params = BParamsBuilder.post()
                .url(Urls.API_USER_FIND_PWD_CHECK_2)
                .addParam("request_id", requestId)
                .addParam("order_id", orderId)
                .addParam("verify_code", verifyCode)
                .build();
        HttpUtils.getInstance().request(params, ResultResponse.class, listener);
    }

    /**
     * RequestVerifyFirstForSignUp
     *
     * @param requestData
     * @param listener
     */
    public static void RequestVerifyFirstForSignUp(VerifyFirstRequest requestData, ResponseListener<OrderParamResponse> listener) {
        if (requestData == null)
            return;

        IParams params;
        if (requestData.bEmail) {
            params = BParamsBuilder.post()
                    .url(Urls.API_GET_BASIC_VERIFY_CODE_FIRST_EMAIL_URL)
                    .addParam("type", "1")
                    .addParam("email", requestData.account)
                    .addParam("captcha_response", requestData.token)
                    .addParam("captcha_id", requestData.captcha_id)
                    .build();
        } else {
            params = BParamsBuilder.post()
                    .url(Urls.API_GET_BASIC_VERIFY_CODE_FIRST_MOBILE_URL)
                    .addParam("type", "1")
                    .addParam("national_code", requestData.mobileCode)
                    .addParam("mobile", requestData.account)
                    .addParam("captcha_response", requestData.token)
                    .addParam("captcha_id", requestData.captcha_id)
                    .build();
        }
        HttpUtils.getInstance().request(params, OrderParamResponse.class, listener);
    }

    /**
     * 找回密码
     *
     * @param requestData
     * @param listener
     */
    @Deprecated
    public static void RequestFindPasswordCheck(FindPasswordCheckRequest requestData, SimpleResponseListener<RequestIdResponse> listener) {
        if (requestData == null)
            return;

        PostParams params;
        if (requestData.isEmail) {
            params = BParamsBuilder.post()
                    .url(Urls.API_USER_EMAIL_FIND_PWD_CHECK)
                    .addParam("email", requestData.account)
                    .addParam("order_id", requestData.orderId)
                    .addParam("verify_code", requestData.verifyCode)
                    .build();
        } else {
            params = BParamsBuilder.post()
                    .url(Urls.API_USER_MOBILE_FIND_PWD_CHECK)
                    .addParam("national_code", requestData.mobileCode)
                    .addParam("mobile", requestData.account)
                    .addParam("order_id", requestData.orderId)
                    .addParam("verify_code", requestData.verifyCode)
                    .build();
        }

        HttpUtils.getInstance().request(params, RequestIdResponse.class, listener);
    }


    /**
     * 创建子账户
     *
     * @param listener
     */
    public static void subAccountCreate(int accountType, String desc, SimpleResponseListener<SubAccountBean> listener) {
        IParams params = BParamsBuilder.post()
                .url(Urls.API_SUB_ACCOUNT_CREATE)
                .addParam("account_type", accountType)
                .addParam("desc", desc)
                .build();
        HttpUtils.getInstance().request(params, SubAccountBean.class, listener);
    }

    /**
     * 账户列表
     *
     * @param listener
     */
    public static void getAccountList(SimpleResponseListener<SubAccountListResponse> listener) {
        IParams params = BParamsBuilder.post()
                .url(Urls.API_SUB_ACCOUNT_LIST)
                .build();
        HttpUtils.getInstance().request(params, SubAccountListResponse.class, listener);
    }

    /**
     * 账户类型
     *
     * @param listener
     */
    public static void getAccountTypes(SimpleResponseListener<AccountTypesResponse> listener) {
        IParams params = BParamsBuilder.post()
                .url(Urls.API_SUB_ACCOUNT_TYPES)
                .build();
        HttpUtils.getInstance().request(params, AccountTypesResponse.class, listener);
    }

    /**
     * ⼦账户转账
     *
     * @param listener
     */
    public static void subAccountTransfer(String tokenId, String amount, int fromAccountType, int fromAccountIndex, int toAccountType, int toAccountIndex, SimpleResponseListener<ResultResponse> listener) {
        IParams params = BParamsBuilder.post()
                .url(Urls.API_SUB_ACCOUNT_TRANSFER)
                .addParam("token_id", tokenId)
                .addParam("amount", amount)
                .addParam("from_account_type", fromAccountType)
                .addParam("from_account_index", fromAccountIndex)
                .addParam("to_account_type", toAccountType)
                .addParam("to_account_index", toAccountIndex)
                .build();
        HttpUtils.getInstance().request(params, ResultResponse.class, listener);
    }

    /**
     * 账户资产信息
     *
     * @param observer
     */
    public static void RequestAccountAsset(int accountType, int accountIndex, SimpleResponseListener<AssetListResponse> observer) {
        if (UserInfo.isLogin() == false)
            return;

        TradeDataManager.GetInstance().getRequestCurrentBalance(observer);

        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ACCOUNT_GET_LIST)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                .addParam("account_type", accountType)
                .addParam("account_index", accountIndex)
                .build();
        HttpUtils.getInstance().request(params, AssetListResponse.class, observer);
    }


    /**
     * 账户资产信息
     *
     * @param tokenId
     * @param listener
     */
    public static void RequestTokenIdAsset(String tokenId, int accountType, int accountIndex, SimpleResponseListener<AssetDataResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_GET_LIST)
                .addParam("account_id", UserInfo.getDefaultAccountId())
                .addParam("token_ids", tokenId)
                .addParam("account_type", accountType)
                .addParam("account_index", accountIndex)
                .build();
        HttpUtils.getInstance().request(params, AssetDataResponse.class, listener);
    }

    /**
     * 账户资产信息
     *
     * @param tokenId
     * @param listener
     */
    public static void RequestCanTransferAsset(String tokenId, int accountType, int accountIndex, SimpleResponseListener<CanTransferAssetResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_CAN_TRANSFER)
                .addParam("token_id", tokenId)
                .addParam("account_type", accountType)
                .addParam("account_index", accountIndex)
                .build();
        HttpUtils.getInstance().request(params, CanTransferAssetResponse.class, listener);
    }

    /**
     * 获取用户等级信息
     */
    public static void GetUserLevelInfo(SimpleResponseListener<UserLevelInfoResponse> listener){
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_GET_USERLEVEL_INFO)
                .build();
        HttpUtils.getInstance().request(params, UserLevelInfoResponse.class,listener);

    }
}
